package tests;

import common.Untils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class LoginPage extends Untils {
    public static String url = "http://8.155.1.153:8081/user/login";
    String nName = null;

    public LoginPage() {
        super(url);
    }
    public void checkLoginPage() throws IOException {
        //检查页面元素是否是否存在
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"root\"]/div/div[1]/span")));
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[1]/span"));
        driver.findElement(By.xpath("//*[@id=\"username\"]"));
        driver.findElement(By.xpath("//*[@id=\"password\"]"));
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/div[2]/div/div/div/div/span/span[2]"));
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/div[3]/a"));
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/button"));
        screenshot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    public void checkEnroll() throws InterruptedException, IOException {
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/div[3]/a")).click();
        //根据时间生成和姓名登录账号,密码为123456
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd-mm-ss-SS");
        String time = sim.format(System.currentTimeMillis());
        nName = time;
        driver.findElement(By.xpath("//*[@id=\"name\"]")).sendKeys(nName);
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(nName);
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("123456");
        driver.findElement(By.xpath("//*[@id=\"rePassword\"]")).sendKeys("123456");
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/button/span")).click();
        screenshot(Thread.currentThread().getStackTrace()[1].getMethodName());
        Thread.sleep(2000);
        //注册成功跳转登录页面
    }
    public void loginFail() throws IOException, InterruptedException {
        Thread.sleep(000);
        //清空输入框的原始内容
        driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
        Thread.sleep(3000);
        //密码错误时
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(nName);
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/button/span")).click();
        screenshot(Thread.currentThread().getStackTrace()[1].getMethodName());
        Thread.sleep(2000);
        //提示错误的用户名和密码,登录失败
    }
    public void loginSuc() throws InterruptedException, IOException {
//        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"username\"]")));
        //键鼠操作无法清空输入框
//        WebElement ele1 = driver.findElement(By.xpath("//*[@id=\"username\"]"));
//        new Actions(driver).doubleClick(ele1).sendKeys(Keys.DELETE).perform();
//        new Actions(driver).click(ele1).sendKeys(nName).perform();
//        WebElement ele2 = driver.findElement(By.xpath("//*[@id=\"password\"]"));
//        new Actions(driver).doubleClick(ele2).sendKeys(Keys.DELETE).perform();
//        new Actions(driver).click(ele2).sendKeys("123456").perform();
        //普通方法无法清空输入框的原始内容
//        Thread.sleep(10000);
//        driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
//        driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
        //最笨办法--重新加载页面在输入
        driver.get(url);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"username\"]")));
        //输入正确,登陆成功
//        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(nName);//暂时注销
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("1423512");
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("123456");
        screenshot(Thread.currentThread().getStackTrace()[1].getMethodName());
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/button/span")).click();
        //调试的时候页面不跳转,故加此循环,但是还是时好时坏
        while (true) {
            Thread.sleep(3000);
            if(driver.getCurrentUrl().equals("http://8.155.1.153:8081/user/login")){
                driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("1423512");
                driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("123456");
                driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div[2]/div/form/button/span")).click();
            }
            break;
        }
    }
}
